package snail.tiger.com.tigerplayer.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import snail.tiger.com.tigerplayer.R;
import snail.tiger.com.tigerplayer.tools.AudioPlayerUtil;
import snail.tiger.com.tigerplayer.users.User;
import snail.tiger.com.tigerplayer.callbacks.BetsChangeCallback;

public class BetsView extends LinearLayout {

    private Context mContext;

    public BetsView(Context context) {
        super(context);
        init(context, null);
    }

    public BetsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BetsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private TextView mTxtBetsName;

    private TextView mTxtBetsOdds;

    private TextView mTxtBetsNum;

    private void init(Context context, AttributeSet attrs) {
        this.mContext = context;

        View view = View.inflate(context, R.layout.bets_view, this);
        mTxtBetsName = view.findViewById(R.id.bets_name);
        mTxtBetsOdds = view.findViewById(R.id.bet_odds);
        mTxtBetsNum = view.findViewById(R.id.bets_num);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BetsView);
        mTxtBetsName.setText(typedArray.getString(R.styleable.BetsView_betName));
        mTxtBetsName.setTextColor(typedArray.getColor(R.styleable.BetsView_betNameColor, Color.parseColor("#0088ff")));
        mTxtBetsName.setBackgroundColor(typedArray.getColor(R.styleable.BetsView_betNameBg, Color.parseColor("#0088ff")));

        if (typedArray.hasValue(R.styleable.BetsView_betOddsColor)) {
            mTxtBetsOdds.setTextColor(typedArray.getColor(R.styleable.BetsView_betOddsColor, Color.parseColor("#0088ff")));
        }

        if (typedArray.hasValue(R.styleable.BetsView_betNumColor)) {
            mTxtBetsNum.setTextColor(typedArray.getColor(R.styleable.BetsView_betNumColor, Color.parseColor("#0088ff")));
        }

        typedArray.recycle();

        // 下注的点击操作
        mTxtBetsName.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    updateAddOrSubtract(v.getId());    //手指按下时触发不停的发送消息
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    stopAddOrSubtract();    //手指抬起时停止发送
                }
                return true;
            }
        });
    }

    private ScheduledExecutorService scheduledExecutor;

    private void updateAddOrSubtract(int viewId) {
        final int vid = viewId;
        scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = vid;
                handler.sendMessage(msg);
            }
        }, 0, 150, TimeUnit.MILLISECONDS);    //每间隔100ms发送Message
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int viewId = msg.what;
            switch (viewId) {
                case R.id.bets_name:
                    if (User.SCORE <= 0 || User.IsRunning || mBetsNum == 99) {
                        return;
                    }
                    AudioPlayerUtil.getInstance().playSound(13, 2);
                    mBetsNum++;// 下注额增加
                    User.SCORE--;// 计分减少
                    betsChangeCallback.onBetsChangeCallback();
                    showBetsNum();
                    break;
            }
        }
    };

    private void stopAddOrSubtract() {
        if (scheduledExecutor != null) {
            scheduledExecutor.shutdownNow();
            scheduledExecutor = null;
        }
    }

    private int mBetsNum;// 下注额
    private int mBetsOdds;// 赔率

    public int getBetsNum() {
        return mBetsNum;
    }

    public void setBetsNum(int mBetsNum) {
        this.mBetsNum = mBetsNum;
        showBetsNum();
    }

    public int getBetsOdds() {
        return mBetsOdds;
    }

    public void setBetsOdds(int mBetsOdds) {
        this.mBetsOdds = mBetsOdds;
        showBetsOdds();
    }

    /**
     * 记录押注数，用来续押
     */
    private int mCacheBetsNum;

    /**
     * 记住押注额，为了续押
     */
    public void rememberBet() {
        mCacheBetsNum = mBetsNum;
    }

    public int getCacheBetsNum() {
        return mCacheBetsNum;
    }

    /**
     * 续押
     */
    public void continueBet() {
        setBetsNum(mCacheBetsNum);
        User.SCORE -= mCacheBetsNum;
    }

    /**
     * 新押
     */
    public void newBet() {
        mCacheBetsNum = 0;
    }

    /**
     * 显示赔率
     */
    public void showBetsOdds() {
        if (mBetsOdds < 10) {
            mTxtBetsOdds.setText("0" + mBetsOdds);
        } else {
            mTxtBetsOdds.setText(String.valueOf(mBetsOdds));
        }
    }

    /**
     * 显示下注额
     */
    public void showBetsNum() {
        if (mBetsNum < 10) {
            mTxtBetsNum.setText("0" + mBetsNum);
        } else {
            mTxtBetsNum.setText(String.valueOf(mBetsNum));
        }
    }

    /**
     * 下注额改变，老虎机界面变化的接口
     */
    private BetsChangeCallback betsChangeCallback;

    public void setBetsChangeCallback(BetsChangeCallback betsChangeCallback) {
        this.betsChangeCallback = betsChangeCallback;
    }
}
